package top.infopub.mgr.bpa.service.impl;


import java.util.Arrays;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.bpa.dao.ProSettleAttDao;
import top.infopub.mgr.bpa.dao.ProSettleDao;
import top.infopub.mgr.bpa.dao.ProSettleDetailDao;
import top.infopub.mgr.bpa.dao.ProSettleFeeDao;
import top.infopub.mgr.bpa.domain.ProSettleDomain;
import top.infopub.mgr.bpa.service.ProSettleService;
import top.infopub.mgr.bpa.vo.ProSettleVo;
import top.infopub.mgr.flow.service.WsFlowProcessService;
import top.infopub.model.bpa.ProSettle;
import top.infopub.model.bpa.ProSettleAtt;
import top.infopub.model.bpa.ProSettleDetail;
import top.infopub.model.bpa.ProSettleFee;
import top.infopub.tdp.util.BizParaUtil;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;


@Service
public class ProSettleServiceImpl extends GenericServiceImpl<ProSettle, String> implements ProSettleService {

    @Autowired
    private ProSettleDao proSettleDao;

    @Autowired
    private ProSettleDetailDao proSettleDetailDao;

    @Autowired
    private ProSettleFeeDao proSettleFeeDao;

    @Autowired
    private ProSettleAttDao proSettleAttDao;

    @Autowired
    private WsFlowProcessService wsFlowProcess;

    @Override
    public GenericDao<ProSettle, String> getDao() {
        return proSettleDao;
    }

    @Override
    public Page<ProSettleDomain> selectByDomainWhere(Page<ProSettleDomain> page,
                                                     ProSettleVo proSettleVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<ProSettleDomain>)proSettleDao.selectByDomainWhere(proSettleVo);
    }

    @Override
    public Page<ProSettleDomain> selectMyCheckByDomainWhere(Page<ProSettleDomain> page,
                                                            ProSettleVo proSettleVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<ProSettleDomain>)proSettleDao.selectMyCheckByDomainWhere(proSettleVo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveProSettle(ProSettle proSettle, List<ProSettleDetail> proSettleDetails,
                              List<ProSettleAtt> proSettleAtts, ProSettleFee proSettleFee) {
        //主表添加
        proSettleDao.insertById(proSettle);

        if (CollectionUtils.isNotEmpty(proSettleDetails)) {
            proSettleDetailDao.insertByBatch(proSettleDetails);
        }

        proSettleFeeDao.insertById(proSettleFee);

        if (CollectionUtils.isNotEmpty(proSettleAtts)) {
            proSettleAttDao.insertByBatch(proSettleAtts);
        }

        String srcBizTable = "BPA_PRO_SETTLE";
        String branchCode_xm = "预算类型 in (types_inner)";
        String branchCode_super = "预算类型 in (types_outer)";
        String outer_biz = BizParaUtil.get("flow_bpa_pro_settle_types");// 需要流转到总部审批的types
        List<String> outer = Arrays.asList(outer_biz.split(","));
        String type = proSettle.getBudgetType();// 预算类型
        String templateId = "";
        // 流转到总部 
        if (outer.contains(type)) {
            templateId = wsFlowProcess.queryTemplateIdByTableBranch(srcBizTable, branchCode_super);
        }
        else {
            templateId = wsFlowProcess.queryTemplateIdByTableBranch(srcBizTable, branchCode_xm);
        }
        // 处理工作流数据
        wsFlowProcess.launch(templateId, srcBizTable, proSettle.getId(), proSettle.getProcessId(), null);
    }

    @Override
    public ProSettleVo queryById(String id) {
        return proSettleDao.queryById(id);
    }

}